package 反射;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.beans.MethodDescriptor;
import java.lang.reflect.Executable; //导入依赖的package包/类
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;
import java.util.function.Function;

public class Executable示例{
	public static void main(String[] agrs) {
		public Parameter[] getParameters(Executable executable) {
			return executable.getParameters();
		}
	

		Parameter[] getParameters() {
			return executable.getParameters();
		}

		/**
		 * Return the {@link Parameter} descriptor for method/constructor parameter.
		 * @since 5.0
		 */
		public Parameter getParameter() {
			if (this.parameterIndex < 0) {
				throw new IllegalStateException("Cannot retrieve Parameter descriptor for method return type");
			}
			Parameter parameter = this.parameter;
			if (parameter == null) {
				parameter = getExecutable().getParameters()[this.parameterIndex];
				this.parameter = parameter;
			}
			return parameter;
		}

		Class<String> clz = String.class;
		for (Method m : clz.getDeclaredMethods()) {
			System.err.println(m.getName());
			for (Parameter p : m.getParameters()) {
				System.err.println("  " + p.getName());
			}
		}

/**
 * Return the {@link Parameter} descriptor for method/constructor parameter.
 * @since 5.0
 */
public Parameter getParameter() {
  if (this.parameterIndex < 0) {
    throw new IllegalStateException("Cannot retrieve Parameter descriptor for method return type");
  }
  Parameter parameter = this.parameter;
  if (parameter == null) {
    parameter = getExecutable().getParameters()[this.parameterIndex];
    this.parameter = parameter;
  }
  return parameter;
}

protected static int findParameterIndex(Parameter parameter) {
  Executable executable = parameter.getDeclaringExecutable();
  Parameter[] allParams = executable.getParameters();
  // Try first with identity checks for greater performance.
  for (int i = 0; i < allParams.length; i++) {
    if (parameter == allParams[i]) {
      return i;
    }
  }
  // Potentially try again with object equality checks in order to avoid race
  // conditions while invoking java.lang.reflect.Executable.getParameters().
  for (int i = 0; i < allParams.length; i++) {
    if (parameter.equals(allParams[i])) {
      return i;
    }
  }
  throw new IllegalArgumentException("Given parameter [" + parameter +
      "] does not match any parameter in the declaring executable");
}

//import java.lang.reflect.Method;
//import java.lang.reflect.Parameter;
//import java.util.ArrayList;
//import java.util.List;
public final class Methods {
  public static List<String> getParameterNames(Method method) {
    Parameter[] parameters = method.getParameters();
    List<String> parameterNames = new ArrayList<>();
    for (Parameter parameter : parameters) {
      if(!parameter.isNamePresent()) {
        throw new IllegalArgumentException("Parameter names are not present!");
      }
      String parameterName = parameter.getName();
      parameterNames.add(parameterName);
    }
    return parameterNames;
  }
  private Methods(){}
}

@Override
public List<RequestParam> parameters(final Executable exec) {
 Parameter[] parameters = exec.getParameters();
 if (parameters.length == 0) {
  return Collections.emptyList();
 }
 Builder<RequestParam> builder = ImmutableList.builder();
 for (Parameter parameter : parameters) {
  builder.add(new RequestParam(parameter, provider.name(parameter)));
 }
 return builder.build();
}

protected static int findParameterIndex(Parameter parameter) {
  Executable executable = parameter.getDeclaringExecutable();
  Parameter[] allParams = executable.getParameters();
  // Try first with identity checks for greater performance.
  for (int i = 0; i < allParams.length; i++) {
    if (parameter == allParams[i]) {
      return i;
    }
  }
  // Potentially try again with object equality checks in order to avoid race
  // conditions while invoking java.lang.reflect.Executable.getParameters().
  for (int i = 0; i < allParams.length; i++) {
    if (parameter.equals(allParams[i])) {
      return i;
    }
  }
  throw new IllegalArgumentException("Given parameter [" + parameter +
      "] does not match any parameter in the declaring executable");
}

/**
 * Get the underlying {@link Parameter}.
 *
 * @return the {@link Parameter}
 */
default Parameter getJavaParameter() {
  Member member = getDeclaringCallable().getJavaMember();
  if (!(member instanceof Executable)) {
    throw new IllegalStateException("Parameter does not belong to an executable: " + member);
  }
  Executable executable = (Executable) member;
  return executable.getParameters()[getPosition()];
}

public String name(final Parameter parameter) {
 String name = RequestParam.nameFor(parameter);
 if (name != null) {
  return name;
 }
 // asm
 Executable exec = parameter.getDeclaringExecutable();
 Parameter[] params = exec.getParameters();
 int idx = IntStream.range(0, params.length)
   .filter(i -> params[i].equals(parameter))
   .findFirst()
   .getAsInt();
 String[] names = nameProvider.names(exec);
 return names[idx];
}

return (index == 0 ? EMPTY_ANNOTATED_ELEMENT : executable.getParameters()[index - 1]);

Parameter[] parameters = executable.getParameters();
Object[] values = new Object[parameters.length];
int start = 0;

boolean hasBody = Arrays.asList(p.getDeclaringExecutable().getParameters()).stream()
  .filter(it -> Stream.of(it.getAnnotations())
    .filter(e -> e.annotationType() == Body.class)

this.messageType = messageType;
ReflectionUtils.ensureAccessible(this.executable);
Parameter[] parameters = executable.getParameters();
this.parameterCount = executable.getParameterCount();

 
parameterResolvers = new ParameterResolver[parameterCount];

boolean implicitRequestObjectAnnotation,
                      boolean isServiceMethod) {
final Parameter[] parameters = constructorOrMethod.getParameters();
if (parameters.length == 0) {
  throw new NoParameterException(constructorOrMethod.toGenericString());
代码示例来源：origin: org.junit.jupiter/junit-jupiter-engine

"A ParameterContext should never be created for parameter index 0 in an inner class constructor");
return executable.getParameters()[this.index - 1];

PetiteInject petiteInject = methodOrCtor.getAnnotation(PetiteInject.class);
final Parameter[] parameters = methodOrCtor.getParameters();

private static List<String> getParameterNames(Executable executable) {
 final List<String> names = new ArrayList<String>();
 final Parameter[] params = executable.getParameters();
 for (Parameter param : params) {
  names.add(param.getName());
 }
 return names;
}

private static List<String> getParameterNames(Executable executable) {
 final List<String> names = new ArrayList<>();
 final Parameter[] params = executable.getParameters();
 for (Parameter param : params) {
  names.add(param.getName());
 }
 return names;
}